package message;

import java.nio.BufferOverflowException;
import java.nio.ByteBuffer;
import java.util.function.BiConsumer;

public class BasicMessageParser<T> extends AbstractMessageParser{

	private BiConsumer<? super ByteBuffer, ? super T> putter;
	
	public BasicMessageParser(BiConsumer<? super ByteBuffer, ? super T> putter, Object obj) {
		super(obj);
		this.putter = putter;
	}
	
	@Override
	@SuppressWarnings("unchecked")
	public boolean write(ByteBuffer byteBuffer) {
		try {
			putter.accept(byteBuffer, (T)object);
		} catch (BufferOverflowException e) {
			return false;
		}
		return true;
	}

}
